package legumes.shared.domain;

import java.io.Serializable;

import javax.persistence.Embedded;
import javax.persistence.Id;


public class OrderPartProductItem implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Id private long id;
	private OnSaleProduct onSaleProduct;
	@Embedded private Quantity quantity;

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	
	public Quantity getQuantity() {
		return quantity;
	}

	public void setQuantity(Quantity quantity) {
		this.quantity = quantity;
	}

	public Cost computePrice() {
		Unit orderUnit = getQuantity().getUnitAsEnum();
		Unit productUnit = getOnSaleProduct().getPrice().getQuantity()
				.getUnitAsEnum();

		float price = orderUnit
				.convertTo(getQuantity().getValue(), productUnit)
				* getOnSaleProduct().getPrice().getAmount()
				/ getOnSaleProduct().getPrice().getQuantity().getValue();

		return new Cost(price);
	}

	public OnSaleProduct getOnSaleProduct() {
		return onSaleProduct;
	}

	public void setOnSaleProduct(OnSaleProduct onSaleProduct) {
		this.onSaleProduct = onSaleProduct;
	}

	public void plus(MenuItem mi_) {
		if (!getOnSaleProduct().equals(mi_.getOnSaleProduct())) {
			return;
		}
		getQuantity().plus(mi_.getQuantity());
	}

	public void minus(MenuItem mi_) {
		if (!getOnSaleProduct().equals(mi_.getOnSaleProduct())) {
			return;
		}
		getQuantity().minus(mi_.getQuantity()).getValue();
	}

	@Override
	public String toString() {
		return "[OrderPartProductItem" + " onSaleProduct="
				+ getOnSaleProduct().getProduct().getName() + " quantity="
				+ getQuantity() + "]";
	}

}
